package mygang.controller;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import mygang.helper.JsonHelper;
import mygang.model.constructor.Form;
import mygang.model.constructor.Step;
import mygang.model.constructor.field.Field;
import mygang.service.ConstructorService;
import mygang.service.manager.FieldService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
 * date: 16/04/2013
 */
@Controller
@RequestMapping(AbstractController.WIZARD_HANDLER)
public class WizardHandler extends AbstractController {

    public static final String WIZARD_GET = "/get";
    public static final String WIZARD_SAVE = "/s";
    
    private static final Logger log = Logger.getLogger(WizardHandler.class.toString());
    @Autowired private FieldService fieldService;
    @Autowired private ConstructorService constructorService;

    @RequestMapping(value = WIZARD_SAVE, method = RequestMethod.POST)
    public @ResponseBody String save(@RequestParam(required = true, value = "step") String step) throws IOException {
        try{
            log.debug(step);
            // prepares all variables
            Gson gson = new Gson();
            JsonParser jsonParser = new JsonParser();
            
            JsonElement jsonElement = jsonParser.parse(step);
            if(jsonElement.isJsonObject()){
                Step dataToStore = gson.fromJson(step, Step.class);
                Set<Field> fields = dataToStore.getFields();
                for(Field field : fields){
                    log.debug(field.getId()+" --- "+field.getValue());
                    field.setEntityId(getCurrentUser().getId());
                    fieldService.saveValue(field);
                }
            }
            return "ok";
        } catch (Exception ex){
            ex.printStackTrace();
            return ex.getMessage();
        }
    }
    
    @RequestMapping(value = WIZARD_GET, method = RequestMethod.POST)
    public void get(@RequestParam(required = false) long id, HttpServletResponse response) throws IOException {
        Form form = constructorService.getForm(id, 0);
        for(Step step : form.getSteps()){
            for(Field field : step.getFields()){
                field.setEntityId(getCurrentUser().getId());
                fieldService.fillValue(field);
            }
        }
        JsonHelper.prepareJsonResponse(form, response);
    }
    
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ModelAndView view(@PathVariable("id") Long id) {
        ModelAndView model = new ModelAndView("wizard");
        model.addObject("id", id);
        return model;
    }
}